package com.woniuxy.config;




import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class RabbitConfig {

    public static final String EXCHANGE_NAME = "log.topic.exchange";
    public static final String QUEUE_DB = "log.queue.db";
    public static final String QUEUE_CACHE = "log.queue.cache";
    public static final String ROUTING_KEY_DB = "log.db";
    public static final String ROUTING_KEY_CACHE = "log.cache";

    // 死信交换机配置
    public static final String DEAD_LETTER_EXCHANGE = "log.dead.exchange";
    public static final String DEAD_LETTER_QUEUE = "log.dead.queue";
    public static final String DEAD_LETTER_ROUTING_KEY = "log.dead";

    /**
     * 创建一个邮件队列
     */
    @Bean
    public Queue emailQueue() {
        return new Queue("MyEmail.queue");
    }

    /**
     *创建日志相关交换机，路由，队列
     */

    // 正常业务交换机
    @Bean
    public TopicExchange logExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    // 死信交换机
    @Bean
    public TopicExchange deadLetterExchange() {
        return new TopicExchange(DEAD_LETTER_EXCHANGE);
    }

    // 主业务队列：log.queue.db，添加死信配置
    @Bean
    public Queue logDbQueue() {
        Map<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key", DEAD_LETTER_ROUTING_KEY);
        args.put("x-message-ttl", 3000); // 每条消息最多存活时间（毫秒），即每次重试间隔约3秒
        return new Queue(QUEUE_DB, true, false, false, args);
    }

    // 普通缓存队列
    @Bean
    public Queue logCacheQueue() {
        Map<String,Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange",RabbitConfig.DEAD_LETTER_EXCHANGE);
        args.put("x-dead-letter-routing-key", RabbitConfig.DEAD_LETTER_ROUTING_KEY);
        args.put("x-message-ttl", 3000);

        return new Queue(QUEUE_CACHE, true, false, false, args);
    }

    // 死信队列
    @Bean
    public Queue deadLetterQueue() {
        return new Queue(DEAD_LETTER_QUEUE);
    }

    // 队列绑定关系
    @Bean
    public Binding bindDbQueue() {
        return BindingBuilder.bind(logDbQueue()).to(logExchange()).with(ROUTING_KEY_DB);
    }

    @Bean
    public Binding bindCacheQueue() {
        return BindingBuilder.bind(logCacheQueue()).to(logExchange()).with(ROUTING_KEY_CACHE);
    }

    @Bean
    public Binding deadLetterBinding() {
        return BindingBuilder.bind(deadLetterQueue()).to(deadLetterExchange()).with(DEAD_LETTER_ROUTING_KEY);
    }

    /**
     * 消息序列化成JSON字符串
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
